package cn.shiwei.hr.web.controller;

import cn.shiwei.hr.service.ICourseTypeService;
import cn.shiwei.hr.domain.CourseType;
import cn.shiwei.hr.query.CourseTypeQuery;
import cn.shiwei.hr.repository.AjaxResult;
import cn.shiwei.hr.repository.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courseType")
public class CourseTypeController {
    @Autowired
    public ICourseTypeService courseTypeService;

    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseType courseType){
        if(courseType.getId()!=null){
            courseTypeService.updateById(courseType);
        }else{
            courseTypeService.insert(courseType);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        courseTypeService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(courseTypeService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(courseTypeService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody CourseTypeQuery query){
        Page<CourseType> page = new Page<CourseType>(query.getPage(),query.getRows());
        page = courseTypeService.selectPage(page);
        PageList pageList = new PageList<CourseType>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
     * 获取treeData
     */
    @RequestMapping(value = "/treeData",method = RequestMethod.GET)
    public AjaxResult getTreeData(){
        return AjaxResult.me().setResultObj(courseTypeService.selectTreeData());
    }

    /**
     * @return
     */
    @RequestMapping(value = "/selectChildrenById/{id}",method = RequestMethod.GET)
    public AjaxResult selectChildrenById(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj("还没写");
    }

    /**
     * 获取面包屑
     */
    @GetMapping(value = "/crumbs/{id}")
    public AjaxResult selectCrumbs(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(courseTypeService.selectCrumbs(id));
    }
}
